package com.jintoufs.weixin;

import com.jintoufs.domain.ad.Ad;
import com.jintoufs.domain.category.Category;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.constants.MessageConstants;
import com.jintoufs.domain.sys.Message;
import com.jintoufs.service.activiti.ActivitiService;
import com.jintoufs.service.ad.AdService;
import com.jintoufs.service.banner.BannerService;
import com.jintoufs.service.category.CategoryService;
import com.jintoufs.service.coupon.CouponService;
import com.jintoufs.service.hotel.HotelService;
import com.jintoufs.service.message.MessageService;
import com.jintoufs.service.scene.SceneService;
import com.jintoufs.service.tour.TourRouteService;
import com.jintoufs.util.valid.ValidUtil;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by administrator on 2017/12/25.
 */
@Controller
@Scope("prototype")
@RequestMapping("api/index")
public class IndexAction extends SupportAction {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private HotelService hotelService;
    @Autowired
    private SceneService sceneService;
    @Autowired
    private AdService adService;
    @Autowired
    private ActivitiService activitiService;
    @Resource
    private CouponService couponService;

    @Resource
    private TourRouteService tourRouteService;

    @GetMapping("/index")
    @ResponseBody
    public AjaxMsg index(String customerId) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        Integer pageStart=0;
        Integer pageSize = 6;
        try {
            //banner查询
            Map<String, Object> result = new HashMap<>();
            Map<String, Object> map = new HashMap();
            map.put("typeId", Constants.BANNER_TYPE_INDEX);
            map.put("display", 1);

            List<Map<String, Object>> banners = bannerService.queryByParams(map);

            //菜单
            Map<String, Object> params = new HashMap();
            params.put("level", Constants.CATEGORY_LEVEL_1);
            List<Category> categoryList = categoryService.queryByParams(params);

            //消息
            Map<String, Object> message_params = new HashMap();
            message_params.put("limit", 5);
            List<Message> messageList =messageService.queryByParams(message_params);

            //活动
            Map<String, Object> activiti_params = new HashMap();
            activiti_params.put("pageStart", 0);
            activiti_params.put("pageSize", 5);
            List activitiList=activitiService.selectByParams(activiti_params);

            //酒店推荐
            Map<String, Object> hotel_params = new HashMap();
            hotel_params.put("status",Constants.RES_STATUS_NORMAL);
            hotel_params.put("inUse",Constants.RES_STATUS_NORMAL);
            hotel_params.put("pageStart", pageStart);
            hotel_params.put("pageSize", pageSize);
            List<Map<String,Object>> hotels = hotelService.queryByParams(hotel_params);

            //鱼·定制推荐
            Map<String,Object> scene_params = new HashMap();
            scene_params.put("pageStart", pageStart);
            scene_params.put("pageSize", pageSize);
            scene_params.put("status",Constants.RES_STATUS_NORMAL);

            List<Map<String,Object>> recommendList = sceneService.getBySearch(scene_params);

            //线路
            Map tour_param = new HashMap();
//            tour_param.put("recommend",Constants.PRODUCT_RECOMMEND);
            tour_param.put("agencyStatus",Constants.RES_STATUS_NORMAL);
            tour_param.put("status",Constants.RES_STATUS_NORMAL);
            tour_param.put("pageStart", pageStart);
            tour_param.put("pageSize", pageSize);
            List<Map> tourList = tourRouteService.getByParams(tour_param);

            //优惠券
            Map couponParam = new HashMap();
            couponParam.put("customerId", customerId);
            List<Map> coupons = couponService.queryByParams(couponParam);
            Ad ad=adService.getLastedAd();


            result.put("couponList", coupons);
            result.put("banner", banners);
            if(ad!=null&&ad.getEnabled()==1){
                result.put("ad", ad);
            }
            result.put("categoryList", categoryList);
            result.put("messageList", messageList);
            result.put("hotelList", hotels);
            result.put("sceneList", recommendList);
            result.put("activitiList", activitiList);
            result.put("tourList", tourList);
            ajaxMsg.setDatas(result);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setMessage("error");
            ajaxMsg.setCode(AjaxMsg.FAILURE);
        }
        return ajaxMsg;
    }

    @GetMapping("/hotel")
    @ResponseBody
    public AjaxMsg getHotels(Integer pageSize,Integer pageNum){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Map result = new HashMap();
        Map<String, Object> hotel_params = new HashMap();
        hotel_params.put("status",Constants.CUSTOMER_AUTH_STATUS_AUTHED);
        hotel_params.put("pageStart", (pageNum - 1) * pageSize);
        hotel_params.put("pageSize", pageSize);
        //根据推荐排序，推荐的放在前面，
        List<Map<String,Object>> hotels = hotelService.queryByParams(hotel_params);
        result.put("hotels", hotels);
        ajaxMsg.setDatas(result);
        return ajaxMsg;
    }

    @GetMapping("/scene")
    @ResponseBody
    public AjaxMsg getScene(Integer pageSize,Integer pageNum){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Map result = new HashMap();
        Map tour_param = new HashMap();
//        tour_param.put("agencyStatus",Constants.CUSTOMER_AUTH_STATUS_AUTHED);
        tour_param.put("pageStart", (pageNum - 1) * pageSize);
        tour_param.put("pageSize", pageSize);
        tour_param.put("status",Constants.RES_STATUS_NORMAL);

        List<Map<String,Object>> tours = sceneService.getBySearch(tour_param);
       // List<Map> tours = tourRouteService.getByParams(tour_param);
        result.put("scenes", tours);
        ajaxMsg.setDatas(result);
        return ajaxMsg;
    }

    @GetMapping("/tour")
    @ResponseBody
    public AjaxMsg getTour(Integer pageSize,Integer pageNum){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Map result = new HashMap();
        Map<String,Object> scene_params = new HashMap();
        scene_params.put("pageStart", (pageNum - 1) * pageSize);
        scene_params.put("pageSize", pageSize);
        List<Map<String,Object>> tours = sceneService.getRecommends(scene_params);
        result.put("tours", tours);
        ajaxMsg.setDatas(result);
        return ajaxMsg;
    }

}
